//
//  ShopcartScrollCell.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/26.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "ShopcartScrollCell.h"

@implementation ShopcartScrollCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
        
        self.cellHeight = 90;
        UIImageView * backgroundImageView= [[UIImageView alloc] initWithImage:[UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth-2*globalMargin, self.cellHeight)]];
        backgroundImageView.size = backgroundImageView.image.size;
        backgroundImageView.y = 0;
        backgroundImageView.x = globalMargin;
        [self.contentView insertSubview:backgroundImageView atIndex:0];
        [backgroundImageView addRadius:10];
        
        
        
        
        
        self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(globalMargin, 0, ScreenWidth*0.75, self.cellHeight)];
        [self.contentView addSubview:self.contentScrollView];
        self.contentScrollView.showsVerticalScrollIndicator = NO;
        self.contentScrollView.showsHorizontalScrollIndicator = NO;
        self.contentScrollView.pagingEnabled = NO;
        
        
        self.countLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.countLabel.font = [UIFont systemFontOfSize:12];
        self.countLabel.textColor = RGBColor(47, 47, 47);
        self.countLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.countLabel];
    }
    return self;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.countLabel.size = [self.countLabel.text sizeInAre:MaxSize font:self.countLabel.font];
    self.countLabel.x = ScreenWidth-7*globalMargin;
    self.countLabel.centerY = self.contentScrollView.centerY;
}
-(void)setCount:(NSInteger)count{
    _count = count;
    self.countLabel.text = [NSString stringWithFormat:@"共%ld件",[ShopCartManager selectedCount]];
    CGFloat width = (self.contentScrollView.width-20)/4;
    CGFloat heught = self.cellHeight*0.75;
    for (int i = 0; i<_count; i++) {
        UIImageView * v = [[UIImageView alloc] initWithFrame:CGRectMake(i*(width+5)+10, self.cellHeight*0.125, width, heught)];
        Product * p = [ShopCartManager shareManager].selectedProduct[i];
        [v sd_setImageWithURL:[NSURL URLWithString:p.small_pic_wap] placeholderImage:nil];
        [self.contentScrollView addSubview:v];
        self.contentScrollView.contentSize = CGSizeMake(v.right, 0);
    }
    [self setNeedsLayout];
}
@end
